import axios from 'axios'
import config from '../config'
import storage from './localstorage'
import { ElMessage } from 'element-plus'
const TOKEN_ERROR = 'Token认证失败,请重新登录'
const NETWORK_ERROR = '网路请求异常,请稍后重试'
const service = axios.create({
  baseURL: config.baseApi,
  timeout: 8000
})

service.interceptors.request.use((req) => {
  // 下面是jwt的token的验证的header 暂且写在这里
  const headers = req.headers
  const {token} = storage.getItem('userInfo')
  if (!headers.Authorization) headers.Authorization = 'Bearer ' + token
  return req
})

service.interceptors.response.use((res) => {
  const {data, code, msg} = res.data
  if (code === 200) {
    return data
  } else if (code === 50001) {
    ElMessage.error(TOKEN_ERROR)
    return Promise.reject(TOKEN_ERROR)
  } else {
    ElMessage.error(msg || NETWORK_ERROR)
    return Promise.reject(msg || NETWORK_ERROR)
  }
})

/**
 * 请求的核心函数
 *  @param {*} options 请求配置
 *  @returns
 */ 
function request (options) {
  options.method = options.method || 'get'
  if (options.method.toLowerCase() === 'get') {
    // 后面组件中在调用接口的时候 传过来的都在对象的data属性中
    options.params = options.data
  }
  let isMock = config.mock
  if (typeof options.mock !== 'undefined') {
    isMock = options.mock
  }
  // 这一点很重要 如果是线上环境这里的baseApi 一定不能出错 不然一上线就是 p0级bug
  if (config.env === 'prod') {
    service.defaults.baseURL = config.baseApi
  } else {
    service.defaults.baseURL = isMock ? config.mockApi : config.baseApi
  }
  return service(options)
}
['get', 'post', 'put', 'delete', 'patch'].forEach(item => {
  request[item] = (url, data, options) => {
    return request({
      url,
      data,
      method: item,
      ...options
    })
  }
})

export default request